<?php
class SG {
  function __construct()
  {
    
  }
  
  function SG()
  {
    
  }
  
  /**
   * Obtiene toda la info de la relacion usuario-curso, o falso si no hay
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   */
  function get($subject_id, $user_id = null)
  {
    $CI =& get_instance();
    
    if($user_id === null && $CI->authentication->logged_in()) {
      $user_id = $CI->authentication->user['user']['id_user'];
    }
    else {
      return FALSE;
    }
    
    $CI->load->model('Permission');
    $info = $CI->Permission->get_permissions($user_id, $subject_id);
    
    if(count($info) == 0) {
      return false;
    }
    else {
      return $info[0];
    }
  }
  
  /**
   * Retorna permisos o falso
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   *
   * @deprecated
   */
  /*function get_permissions($subject_id, $user_id = null)
  {
    $CI =& get_instance();
    
    if($user_id === null && $CI->authentication->logged_in()) {
      $user_id = $CI->authentication->user['user']['id_user'];
    }
    else {
      return FALSE;
    }
    
    $CI->load->model('Permission');
    $permissions = $CI->Permission->get_permissions($user_id, $subject_id);
    
    if(count($permissions) == 0) {
      return false;
    }
    else {
      return $permissions[0];
    }
  }*/
  
  /**
   * DRY approach a los permisos
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @param string $what 
   * @return void
   * @author Ian Murray
   */
  function can($subject_id, $user_id, $what)
  {
    /* Si es admin, si puede */
    if($this->is_admin()) {
      return TRUE;
    }
    
    if( ! ($permissions = $this->get($subject_id, $user_id)) ) {
      return false;
    }
    
    return ($permissions[$what] == DBYES);
  }
  
  /**
   * Devuelve si el usuario puede postear noticias
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   */
  function can_post($subject_id, $user_id = null) {
    return $this->can($subject_id, $user_id, 'can_post');
  }
  
  /**
   * Revisa si el usuario puede crear una carpeta
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   */
  function can_create_folder($subject_id, $user_id = null)
  {
    return $this->can($subject_id, $user_id, 'can_create_folder');
  }
  
  /**
   * Revisa si puede subir un archivo
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   */
  function can_upload($subject_id, $user_id = null)
  {
    return $this->can($subject_id, $user_id, 'can_upload');
  }
  
  /**
   * Verifica si hay una relacion entre el alumno y el curso basicamente
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   */
  function is_subscribed($subject_id, $user_id = null)
  {
    return ($this->get($subject_id, $user_id) != FALSE);
  }
  
  /**
   * Revisa si el usuario es profesor de el ramo dado
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   */
  function is_teacher($subject_id, $user_id = null) 
  {
    if( ! $options = $this->get($subject_id, $user_id)) {
      return FALSE;
    }
    
    return ($options['type'] == TEACHER);
  }
  
  /**
   * Revisa si el usuario es ayudante de ese ramo
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   */
  function is_ta($subject_id, $user_id = null)
  {
    if( ! $options = $this->get($subject_id, $user_id)) {
      return FALSE;
    }
    
    return ($options['type'] == TA);
  }
  
  /**
   * Devuelve true o false si el usuario recibe notificaciones
   * de noticias de ese ramo.
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   */
  function news_notifications($subject_id, $user_id = null)
  {
    if( ! $options = $this->get($subject_id, $user_id)) {
      return FALSE;
    }
    
    return ($options['news_notifications'] == DBYES);
  }
  
  /**
   * Devuelve true o false si el usuario recibe notificaciones de archivos.
   *
   * @param string $subject_id 
   * @param string $user_id 
   * @return void
   * @author Ian Murray
   */
  function files_notifications($subject_id, $user_id = null)
  {
    if( ! $options = $this->get($subject_id, $user_id)) {
      return FALSE;
    }
    
    return ($options['files_notifications'] == DBYES);
  }
  
  /**
   * Cambia notificaciones
   *
   * @param string $what 
   * @param string $setting 
   * @return void
   * @author Ian Murray
   */
  function set_options($subject_id, $user_id, $changes = array()) 
  {
    $CI =& get_instance();
    
    $CI->load->model('Permission');
    $CI->Permission->set($user_id, $subject_id, $changes);
  }
  
  /**
   * Chequea que el usuario actual sea un admin
   *
   * @return void
   * @author Ian Murray
   */
  function is_admin()
  {
    $CI =& get_instance();
    
    if( ! $CI->authentication->logged_in()) {
      return false;
    }
    
    return ($CI->authentication->user['group']['type'] == ADMIN);
  }

  /**
   * Chequea si el subject (arreglo pasado) puede ser visto por el usuario logueado
   *
   * @param string $subject 
   * @return void
   * @author Ian Murray
   */
  function can_view_subject($subject)
  {
    return ( $this->is_admin() || $subject['private'] == DBNO || ( $subject['private'] == DBYES && $this->is_subscribed($subject['id_subject']) )  );
  }
}
?>